﻿using System.Text.Json.Serialization;

namespace LightCAD.Core.Elements
{
    /// <summary>
    /// 坐标标注
    /// </summary>
    public class DimOrdinate : LcElement
    {

        [JsonInclude, JsonPropertyName("S")]
        [JsonConverter(typeof(Vector2dConverter))]
        public Vector2d Start;
        [JsonInclude, JsonPropertyName("E")]
        [JsonConverter(typeof(Vector2dConverter))]
        public Vector2d End;

        public string Dimtext;

        public DimOrdinate()
        {
            this.Type = BuiltinElementType.DimOrdinate;
        }

        public DimOrdinate(Vector2d start, Vector2d end, string dimtext) : this()
        {
            this.Start = start;
            this.End = end;
            this.Dimtext = dimtext;
        }


        public override LcElement Clone()
        {
            var clone = new DimOrdinate();
            clone.Copy(this);
            return clone;
        }

        public override void Copy(LcElement src)
        {
            var dim = ((DimOrdinate)src);
            this.Start = dim.Start;
            this.End = dim.End;
            this.Dimtext = dim.Dimtext;
        }

        public override Box2d GetBoundingBox()
        {
            return new Box2d(this.Start, this.End);
        }
        public override Box2d GetBoundingBox(Matrix3d matrix)
        {
            return new Box2d(matrix.MultiplyPoint(this.Start), matrix.MultiplyPoint(this.End));
        }

        public override bool IntersectWithBox(Polygon2d testPoly, List<RefChildElement> intersectChildren = null)
        {
            var thisBox = this.BoundingBox;
            if (!thisBox.IntersectWith(testPoly.BoundingBox))
            {
                //如果元素盒子，与多边形盒子不相交，那就可能不相交
                return false;
            }
            return GeoUtils.IsPolygonIntersectLine(testPoly.Points, this.Start, this.End)
                || GeoUtils.IsPolygonContainsLine(testPoly.Points, this.Start, this.End);
        }

        public override bool IncludedByBox(Polygon2d testPoly, List<RefChildElement> includedChildren = null)
        {
            var thisBox = this.BoundingBox;
            if (!thisBox.IntersectWith(testPoly.BoundingBox))
            {
                return false;
            }
            return GeoUtils.IsPolygonContainsLine(testPoly.Points, this.Start, this.End);
        }

    }
}
